package com.angel.socket;

import com.angel.bean.Employee;
import com.angel.bean.EmployeeType;
import com.angel.bean.dto.SocketEmployeeDto;
import com.angel.service.EmployeeService;
import com.angel.service.EmployeeTypeService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectEvent;

import java.security.Principal;
import java.util.Map;

/**
 * websocket连接监听
 *
 * @author Rabit
 */

@Component
public class ConnectEventListener implements ApplicationListener<SessionConnectEvent> {
    @Autowired
    private EmployeeService employeeService;


    @Override
    public void onApplicationEvent(SessionConnectEvent event) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(event.getMessage());
        String sessionid=headerAccessor.getSessionAttributes().get("sessionId").toString();
        Employee employee = (Employee) headerAccessor.getSessionAttributes().get("employee");
        Employee et = new Employee();
        et.setEmId(employee.getEmId());
        SocketEmployeeDto socketEmployeeDto =employeeService.findEmployeeAndType(et);
        if (sessionid!=null && employee!=null){
            //将用户信息存到统一的集合里面
            WebSocketUsers.putEmployee(sessionid,socketEmployeeDto);
        }
        System.out.println("[ConnectEventListener]");
    }
}
